using Game.Networking.Utils;
using TMPro;
using UnityEngine;

namespace Game.UI {
	public sealed class ShipInfoWindow : UIWindow {
		public TMP_Text StatusText;
		public TMP_Text HealthText;
		public TMP_Text PositionText;
		public TMP_Text VelocityText;

		IComputingUnitUserDataProvider _dataProvider;

		private void Start() {
			var isServer = NetworkOptions.StartServer;
			if ( isServer ) {
				_dataProvider = new ServerComputingUnitUserDataProvider();
			}
			else {
				_dataProvider = new ClientComputingUnitUserDataProvider();
			}
		}

		private void Update() {
			var entity = _dataProvider.GetAttachedEntity();
			if ( entity == null ) {
				StatusText.text = "Status: Unknown";
				HealthText.text = "HP: 0";
				PositionText.text = "Position: ---";
				VelocityText.text = "Velocity: 0";
				return;
			}
			StatusText.text = "Status: Active";
			HealthText.text = $"HP: {entity.Health}";
			PositionText.text = $"Position: {Mathf.RoundToInt(entity.transform.position.x)} {Mathf.RoundToInt(entity.transform.position.y)} {Mathf.RoundToInt(entity.transform.position.z)}";
			VelocityText.text = $"Velocity: {Mathf.RoundToInt(entity.Rb.velocity.magnitude)}";
		}
	}
}
